package com.gzdh.dishrecognition.ui.base;


import android.app.Activity;
import android.content.Context;
import android.hardware.display.DisplayManager;
import android.os.Bundle;
import android.view.Display;
import android.view.KeyEvent;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Lifecycle;

import com.blankj.utilcode.util.LogUtils;
import com.trello.lifecycle4.android.lifecycle.AndroidLifecycle;
import com.trello.rxlifecycle4.LifecycleProvider;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * MVC Activity 基类
 */
public abstract class BaseMVCActivity extends AppCompatActivity implements AbstractView {

    //创建lifecycle
    public LifecycleProvider<Lifecycle.Event> lifecycleProvider = AndroidLifecycle.createLifecycleProvider(this);

    private Unbinder unbinder;

    public Activity mActivity;

    //副屏
    public Display mDisplay;
    //主屏
    public Display mMainDisplay;

    public List<BasePresentation> mPresentationList = new ArrayList<>();

    public Bundle mSavedInstanceState;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSavedInstanceState = savedInstanceState;
        mActivity = this;
        DisplayManager displayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
        Display[] presentationDisplays = displayManager.getDisplays();
        Display display = presentationDisplays[0];


        setContentView(getLayoutId());
        //竖屏
//        ScreenUtils.setPortrait(this);
        //注册EventBus
        EventBus.getDefault().register(this);
        //ButterKnife绑定
        unbinder = ButterKnife.bind(this);
        getDisplays();
        onCreateBundle(savedInstanceState);
        //初始化视图
        initView();
        //初始化数据
        initData();
        initPresentation();
        onBusiness();
    }

    /**
     * 获取Create的Bundle
     *
     * @param savedInstanceState
     */
    protected void onCreateBundle(@Nullable Bundle savedInstanceState) {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //销毁EventBus
        EventBus.getDefault().unregister(this);
        //销毁ButterKnife
        if (unbinder != null)
            unbinder.unbind();
        //关闭loading（如果正在显示的话）
        dissmissPresnetation();
        mPresentationList = null;
    }

    /**
     * 获取当前UI布局
     */
    protected abstract int getLayoutId();

    /**
     * 初始化视图
     */
    protected abstract void initView();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    protected abstract void dissmissPresnetation();

    public void initPresentation() {

    }

    public void onBusiness() {


    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(Object event) {
        //分发所有eventbus事件到副屏
        if (mPresentationList != null) {
            for (BasePresentation basePresentation : mPresentationList) {
                basePresentation.onEvent(event);
            }
        }
    }


    @Override
    public Context context() {
        return this;
    }

    @Override
    public AppCompatActivity activity() {
        return this;
    }

    //自定义业务逻辑相关
    public void showPresentation(BasePresentation presentation) {
        if (mDisplay == null)
            return;
        if (presentation != null && presentation.getDisplay() != mDisplay) {
            presentation.dismiss();
        }
        if (presentation != null) {
            try {
                presentation.show();
            } catch (WindowManager.InvalidDisplayException e) {
                LogUtils.e(e);
            }
        }
    }

//    public void showPresentationAtMain(BasePresentation presentation) {
//        if (mMainDisplay == null)
//            return;
//        if (presentation != null && presentation.getDisplay() != mMainDisplay) {
//            presentation.dismiss();
//        }
//        if (presentation != null) {
//            try {
//                presentation.show();
//            } catch (WindowManager.InvalidDisplayException e) {
//                LogUtils.e(e);
//            }
//        }
//    }

    public void getDisplays() {

        DisplayManager displayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
        Display[] presentationDisplays = displayManager.getDisplays();
        mMainDisplay = presentationDisplays[0];
        if (presentationDisplays.length > 1) {
            mDisplay = presentationDisplays[1];
        } else
            mDisplay = null;
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            return onKeyEventBack(event);
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
            return onKeyEventEnter(event);
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP
                || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
            return onKeyEventUpAndDown(event);
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_SETTINGS) {
            return onKeyEventSetting(event);
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_F1) {
            return onKeyEventFunction(event);
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_0
                || event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_1
                || event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_2
                || event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_3
                || event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_4
                || event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_5
                || event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_6
                || event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_7
                || event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_8
                || event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_9
                || event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_DOT
                || event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
            return onKeyEventNum(event);
        }
        return super.dispatchKeyEvent(event);
    }

    /**
     * 取消键
     */
    public boolean onKeyEventBack(KeyEvent event) {
        return super.dispatchKeyEvent(event);
    }

    /**
     * 确认键
     */
    public boolean onKeyEventEnter(KeyEvent event) {
        return super.dispatchKeyEvent(event);
    }

    /**
     * 数字
     */
    public boolean onKeyEventNum(KeyEvent event) {
        return super.dispatchKeyEvent(event);
    }

    /**
     * 向上向下
     */
    public boolean onKeyEventUpAndDown(KeyEvent event) {
        return super.dispatchKeyEvent(event);
    }

    /**
     * 设置
     */
    public boolean onKeyEventSetting(KeyEvent event) {
        return super.dispatchKeyEvent(event);
    }

    /**
     * 功能
     */
    public boolean onKeyEventFunction(KeyEvent event) {
        return super.dispatchKeyEvent(event);
    }

    public void registerPresentation(BasePresentation presentation) {
        if (mPresentationList != null && !mPresentationList.contains(presentation))
            mPresentationList.add(presentation);
    }

    //
    public void unRegPresentation(BasePresentation presentation) {
        if (mPresentationList != null)
            mPresentationList.remove(presentation);
    }
}
